package cn.buk.qms.task;

import cn.buk.tms.hotel.service.HotelOrderService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Profile;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;


/**
 * 定时任务
 *
 * @author yfdai
 */
@Profile({"prod"})
@Component
public class HotelOrderTask {

  private static final int SERVER_THIRD = 3;

  private static final Logger logger = LogManager.getLogger(HotelOrderTask.class);

  /**
   * 当前应用所在服务器的id
   */
  private final int serverId;

  /**
   * 当前应用所属的拥有者在数据库中对应的企业id
   */
  private final int ownerId;

  private String getAgentName() {
    return "system-" + serverId;
  }

  private final HotelOrderService hotelOrderService;

  @Autowired
  public HotelOrderTask(@Value("${server_id}") int serverId,
                        @Value("${owner_enterprise_id}") int ownerId,
                        HotelOrderService hotelOrderService) {
    this.serverId = serverId;
    this.ownerId = ownerId;
    this.hotelOrderService = hotelOrderService;
  }



  /**
   * 每30分钟统计是否有订单需要处理
   */
  @Async
  @Scheduled(cron = "0 0 10,15 * * 1-5")
  public void autoAlertHotelOrderStatus() {
    if (serverId != SERVER_THIRD) {
      return;
    }

    //酒店订单
    hotelOrderService.autoAlertHotelOrderStatus();

  }


  /**
   * 每30秒查找一次需要自动生成编码和验价的
   */
  @Async
  @Scheduled(cron = "0/30 * * * * *")
  public void checkOrderPrice() {
    if (serverId != SERVER_THIRD) {
      return;
    }
    try {
      hotelOrderService.checkHotelOrderPrice(ownerId, getAgentName());
    } catch (Exception ex) {
      logger.error(ex.getMessage());
    }
  }

  /**
   * 每分钟查找一次需要自动分配审批人的订单
   */
  @Async
  @Scheduled(cron = "0 * * * * *")
  public void checkOrderNeedApprover() {
    if (serverId != SERVER_THIRD) {
      return;
    }
    try {
      hotelOrderService.checkHotelOrderNeedApprover(ownerId, getAgentName());
    } catch (Exception ex) {
      logger.error(ex.getMessage());
    }
  }

  /**
   * 每分钟查找一次需要校验是否违反企业差旅规定的订单
   */
  @Async
  @Scheduled(cron = "0 * * * * *")
  public void checkOrderMeetPolicy() {
    if (serverId != SERVER_THIRD) {
      return;
    }

    try {
      hotelOrderService.checkHotelOrderCityId(ownerId, getAgentName());
      hotelOrderService.checkHotelOrderMeetPolicy(ownerId, getAgentName());
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  /**
   * 每20分钟查找一次需要需要自动去掉的酒店订单
   */
  @Async
  @Scheduled(cron = "0 0/20 * * * *")
  public void checkHotelOrderStatus() {
    if (serverId != SERVER_THIRD) {
      return;
    }

    try {
      hotelOrderService.checkHotelOrderStatus(ownerId, getAgentName());

    } catch (Exception ex) {
      logger.error(ex.getMessage());
    }
  }

  /**
   * 每分钟查找一次需要发送通知的酒店订单(客户自助下单的，通知后台客服）
   */
  @Async
  @Scheduled(cron = "0 * * * * *")
  public void checkHotelOrderNotifyStatus() {
    if (serverId != SERVER_THIRD) {
      return;
    }

    try {
      hotelOrderService.checkHotelOrderNotifyStatus(ownerId, getAgentName());

    } catch (Exception ex) {
      logger.error(ex.getMessage());
    }
  }

}
